package mx.kolobok.noteeditor;

import org.jetbrains.annotations.NotNull;

import javax.swing.undo.UndoManager;
import java.awt.*;
import java.util.Date;
import java.util.Observable;

/**
 * User: Nikita Belenkiy
 * Date: 28.01.11
 * Time: 23:03
 */
public class Note extends Observable {
//    private static final Color defaultColor = new Color(0,0,0,255);

    private String title;
    private String text;

    private Color color;
    private Date created;
    private Date updated;

    @NotNull
    private transient UndoManager undoManager = new UndoManager();

    public Note(String title, String text, Date created, Date updated, Color color) {
        this.title = title;
        this.text = text;
        this.created = created;
        this.updated = updated;
        this.color = color;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        if (!title.equals(this.title)) {
            this.title = title;
            touch();
        }
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        if (!this.text.equals(text)) {
            this.text = text;
            touch();
        }
    }

    public Date getCreated() {
        return created;
    }

    public Date getUpdated() {
        return updated;
    }

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        if (!this.color.equals(color)) {
            this.color = color;
            touch();
        }
    }

    /**
     * marks note as edited
     */
    public void touch() {
        this.updated = new Date();
        setChanged();
        notifyObservers();
    }

    @NotNull
    public UndoManager getUndoManager() {
        return undoManager;
    }

    @Override
    public String toString() {
        return text;
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Note note = (Note) o;

        if (!created.equals(note.created)) return false;
        if (!text.equals(note.text)) return false;
        if (!title.equals(note.title)) return false;
        if (!updated.equals(note.updated)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = title.hashCode();
        result = 31 * result + text.hashCode();
        result = 31 * result + created.hashCode();
        result = 31 * result + updated.hashCode();
        return result;
    }
}
